$(function () {
    $.ajax({
        url: "http://apis.map.qq.com/ws/district/v1/list?key=7DFBZ-K4PWQ-TYK5Y-GL7XN-RBDSQ-XSB6M&output=jsonp",
        type: "GET",
        dataType: "JSONP",
        success: function (data) {
            console.log(data);
            var result = data.result[0];
            for(var i = 0;i<result.length;i++)
                $(".provinceSelect").append(
                    "<option value=\"" + result[i].name + "\" id=\"" + result[i].id + "\">" + result[i].name + "</option>"
                );
            $.ajax({
                url: "http://apis.map.qq.com/ws/district/v1/getchildren?key=7DFBZ-K4PWQ-TYK5Y-GL7XN-RBDSQ-XSB6M&output=jsonp&id=" + $(".provinceSelect option:selected").attr("id"),
                type: "GET",
                dataType: "JSONP",
                success: function (data) {
                    var result = data.result[0];
                    for(var i = 0;i<result.length;i++)
                        $(".citySelect").append(
                            "<option value=\"" + result[i].name + "\" id=\"" + result[i].id + "\">" + result[i].name + "</option>"
                        );
                    $.ajax({
                        url: "/school?city=" + $(".citySelect option:selected").attr("value"),
                        type: "GET",
                        success: function (data) {
                            for(var i = 0;i<data.length;i++)
                                $(".schoolSelect").append(
                                    "<option value=\"" + data.name + "\" id=\"" + data.id + "\">" + data.name + "</option>"
                                );
                        }
                    });
                }
            });
        }
    });

    $(".provinceSelect").change(function () {
        $(".citySelect").empty();
        $.ajax({
            url: "http://apis.map.qq.com/ws/district/v1/getchildren?key=7DFBZ-K4PWQ-TYK5Y-GL7XN-RBDSQ-XSB6M&output=jsonp&id=" + $(".provinceSelect option:selected").attr("id"),
            type: "GET",
            dataType: "JSONP",
            success: function (data) {
                var result = data.result[0];
                for(var i = 0;i<result.length;i++)
                    $(".citySelect").append(
                        "<option value=\"" + result[i].name + "\" id=\"" + result[i].id + "\">" + result[i].name + "</option>"
                    );
                $.ajax({
                    url: "/school?city=" + $(".citySelect option:selected").attr("value"),
                    type: "GET",
                    success: function (data) {
                        for(var i = 0;i<data.length;i++)
                            $(".schoolSelect").append(
                                "<option value=\"" + data[i].name + "\" id=\"" + data[i].id + "\">" + data[i].name + "</option>"
                            );
                    }
                });
            }
        });
    });

    $(".citySelect").change(function () {
        $(".schoolSelect").empty();
        $.ajax({
            url: "/school?city=" + $(".citySelect option:selected").attr("value"),
            type: "GET",
            success: function (data) {
                console.log(data);
                for(var i = 0;i<data.length;i++)
                    $(".schoolSelect").append(
                        "<option value=\"" + data[i].name + "\" id=\"" + data[i].id + "\">" + data[i].name + "</option>"
                    );
            }
        });
    });

    $(".submit").click(function () {
        var phone = $("input[name='phoneNum']").val();
        var password = $("input[name='password']").val();
        var name = $("input[name='name']").val();
        var school = $(".schoolSelect option:selected").attr("id");
        var gender = $("input[name='gender']:checked").val();
        var type = $("input[name='role']:checked").val();
        var number = $("input[name='stuffNum']").val();
        var email = $("input[name='eMail']").val();

        console.log(gender);
        console.log(type);

        var data = {
            phone: phone,
            password: password,
            name: name,
            school: school,
            gender: gender == "男" ? 0 : 1,
            type: type == "学生" ? 0 : 1,
            number: number,
            email: email
        };

        console.log(data);

        $.ajax({
            url: "/register",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (data) {
                alert("注册成功!");
                localStorage.setItem("jwt", data.jwt);
                localStorage.setItem("userId", data.id);
                console.log(data);
                if (type == "学生") {
                    window.location.href = "/student/home";
                } else {
                    window.location.href = "/teacher/home";
                }
            }
        })
    });

    $(".createSchool").click(function () {
        window.location.href = "/teacher/createSchool";
    });
});